package com.wenwei.sharebox.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.activity.CateGoryActivity;
import com.wenwei.sharebox.activity.GoodsDetailsActivity;
import com.wenwei.sharebox.activity.QRcodeActivity;
import com.wenwei.sharebox.activity.ShareCommonActivity;
import com.wenwei.sharebox.adapter.HomeSaleAdapter;
import com.wenwei.sharebox.base.BaseFragment;
import com.wenwei.sharebox.base.Constant;
import com.wenwei.sharebox.bean.BannerBean;
import com.wenwei.sharebox.bean.HomeHotGoodsBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.view.FullyGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.badgeview.BGABadgeLinearLayout;
import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Created by Administrator on 2017/7/6.
 */
public class HomeFragment extends BaseFragment implements BGABanner.Adapter {
    @Bind(R.id.home_page_banner)
    BGABanner homePageBanner;
    @Bind(R.id.home_fragment_message)
    BGABadgeLinearLayout messageIv;
    @Bind(R.id.recycler)
    RecyclerView mRecycler;

    private HomeSaleAdapter mAdapter;
    @Override
    public View initView(LayoutInflater inflater) {
        return LayoutInflater.from(mActivity).inflate(R.layout.fragment_home, null);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        messageIv.hiddenBadge();
        mRecycler.setLayoutManager(new FullyGridLayoutManager(mActivity,2));
        mAdapter = new HomeSaleAdapter(new HomeSaleAdapter.CallBack() {
            @Override
            public void onItemClick(int id) {
                Bundle bundle = new Bundle();
                bundle.putInt("goodsId",id);
                goActivity(mActivity,GoodsDetailsActivity.class,bundle);
            }
        });
        mRecycler.setAdapter(mAdapter);
    }

    @Override
    protected void lazyload() {

    }

    @Override
    public void fillBannerItem(BGABanner banner, View itemView, Object model, int position) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.search_rl,R.id.home_cate_rl, R.id.home_sao_rl, R.id.home_buy_rl, R.id.home_zu_rl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.home_cate_rl:
                goActivity(mActivity, CateGoryActivity.class);
                break;
            case R.id.home_sao_rl:
                Bundle checkBundle = new Bundle();
                checkBundle.putInt("qr_type", Constant.QR_TYPE_GOODS);
                goActivity(mActivity, QRcodeActivity.class,checkBundle);
                break;
            case R.id.home_buy_rl:
                Bundle b1 = new Bundle();
                b1.putInt("type",1);
                goActivity(mActivity, ShareCommonActivity.class,b1);
                break;
            case R.id.home_zu_rl:
                Bundle b2= new Bundle();
                b2.putInt("type",0);
                goActivity(mActivity, ShareCommonActivity.class,b2);
                break;
            case R.id.search_rl:
                Bundle b3= new Bundle();
                b3.putInt("type",-1);
                goActivity(mActivity, ShareCommonActivity.class,b3);
                break;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        getBanner();
        getSaleTop8();
    }

    //获取banner
    BaseSubscriber<ArrayList<BannerBean>> bannerSubscriber;
    private void getBanner(){
        bannerSubscriber = new BaseSubscriber<ArrayList<BannerBean>>(mActivity) {
            @Override
            public void onSuccess(ArrayList<BannerBean> bannerBeen) {
                homePageBanner.setAdapter(new BGABanner.Adapter() {
                    @Override
                    public void fillBannerItem(BGABanner banner, View itemView, Object model, int position) {
                        BannerBean bannerBean = (BannerBean) model;
                        Glide.with(mActivity).load(bannerBean.getImgUrl()).into((ImageView) itemView);
                    }
                });
                homePageBanner.setDelegate(new BGABanner.Delegate() {
                    @Override
                    public void onBannerItemClick(BGABanner banner, View itemView, Object model, int position) {
                        try {
                            BannerBean bannerBean = (BannerBean) model;
                            Intent intent= new Intent();
                            intent.setAction("android.intent.action.VIEW");
                            Uri content_url = Uri.parse("http://"+bannerBean.getUrl());
                            intent.setData(content_url);
                            startActivity(intent);
                        }catch (Exception e){}
                    }
                });
                homePageBanner.setData(bannerBeen, null);
            }
        };
        ApiManager.getInstance().getBanner(bannerSubscriber);
    }

    //获取热销商品
    BaseSubscriber<ArrayList<HomeHotGoodsBean>> saleSubscriber;
    private void getSaleTop8(){
        saleSubscriber = new BaseSubscriber<ArrayList<HomeHotGoodsBean>>(mActivity) {
            @Override
            public void onSuccess(ArrayList<HomeHotGoodsBean> homeHotGoodsBean) {
                mAdapter.setSaleData(homeHotGoodsBean);
                mAdapter.notifyDataSetChanged();
            }
        };
        ApiManager.getInstance().getSaleTop8(saleSubscriber);
    }
}
